package com.service;

import com.mapper.AccountMapper;
import com.pojo.Account;
import com.pojo.RightClassTwo;
import com.pojo.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AccountServiceImpl implements AccountService{

    @Autowired
    private AccountMapper accountMapper;

    public List<RightClassTwo> showRightByRightSuperId(Long id,Long superId) {
        return accountMapper.showRightByRightSuperId(id,superId);
    }

    public boolean deleteAccountByUserCode(Long userCode) {
        int row = accountMapper.updateStatusByUserCode(userCode);
        if (row>0){
            return true;
        }
        return false;

    }

    public Account findAccountByUserCode(Long userCode) {
        return accountMapper.findAccountByUserCode(userCode);
    }

    public List<Role> findRoleList(Long roleId,List<Role> list) {
        List<Role> roleList = accountMapper.findRoleList(roleId);
        if (roleList.size()!=0){
            list.addAll(roleList);
            for (Role role :roleList) {
                findRoleList(role.getRoleId(),list);
            }
        }
        return list;
    }

    public boolean updateAccount(Account account) {
        int row = accountMapper.updateAccount(account);
        int i = accountMapper.updateAccountRole(account);
        if (row>0&&i>0){
            return true;
        }
        return false;

    }

    public boolean updatePassWord(Account account) {
        int row = accountMapper.updatePassWord(account);
        if (row>0){
            return true;
        }
        return false;
    }

    public boolean findAccountByUserName(String userName) {
        Account account = accountMapper.findAccountByUserName(userName);
        if (account!=null){
            return true;
        }
        return false;
    }

    public boolean addAccount(Account account) {
        int row = accountMapper.addAccount(account);
        int i=0;
        Account accountByUserName = accountMapper.findAccountByUserName(account.getUserName());
        if (accountByUserName!=null){
            i = accountMapper.addAccountRole(accountByUserName.getUserCode(), account.getRoleId());
        }
        if (row>0&&i>0){
            return true;
        }
        return false;
    }

    public List<Account> findFuzzySearch(Long roleId, Account account) {

        List<Account> list = accountMapper.findAccountListByFuzzy(roleId, account);
        List<Account> group = accountMapper.groupByRoleId(roleId);
        for (Account a :group) {
            List<Account> byFuzzy = accountMapper.findAccountListByFuzzy(a.getRoleId(), account);
            list.addAll(byFuzzy);
        }
        Account fuzzy = accountMapper.findAccountByFuzzy(roleId, account);
        if (fuzzy!=null){
            list.add(0,fuzzy);
        }
        return list;
    }

    public List<Account> findAccountListByFuzzy_1(Long roleId, Account account) {
        return accountMapper.findAccountListByFuzzy_1(roleId,account);
    }

    public List<Account> findAccountListByFuzzy_2(Long roleId, Account account) {
        return accountMapper.findAccountListByFuzzy_2(roleId, account);
    }
}
